#import "UILabelControl.h"
#import "FontFactory.h"
#import "ColorFactory.h"


@implementation CUILabelControl

@synthesize iLabel;

-init
{
	self = [super init];
	if (self)
	{
		self.tag = 0;
	}
	return self;

}
//初始化label
-(void)CustomInit
{
	self.iLabel=[[UILabel alloc ] initWithFrame:CGRectZero];
	self.iLabel.backgroundColor = [UIColor clearColor];
	self.iLabel.lineBreakMode = UILineBreakModeWordWrap;
	self.iLabel.numberOfLines = 0;
	
	UIFont *font;
	font=[CFontFactory CreateFontBySizeAndWeight:self.iFontSize weightTag:self.iFontWeight];
	self.iLabel.font=font;
	//[font release];
	
	if(iText)
	{
		NSString *finalString = [[NSString alloc] initWithString:[iText stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "]];
		self.iLabel.text=finalString;
		[finalString release];
	}
	if(self.iBackColor)
		self.iLabel.backgroundColor=[CColorFactory CreateRgbCorol:self.iBackColor];
	else {
		self.iLabel.backgroundColor = [UIColor clearColor];
	}

//	if(self.iFontColor)
//		iLabel.textColor=[CColorFactory CreateRgbCorol:self.iFontColor];
	self.iLabel.textColor=[UIColor blackColor];
	switch (self.iHorizontal) {
		case 0:
			self.iLabel.textAlignment = UITextAlignmentCenter;
			break;
		case 1:
			self.iLabel.textAlignment = UITextAlignmentLeft;
			break;
		case 2:
			self.iLabel.textAlignment = UITextAlignmentRight;
			break;
	}
	[self addSubview:self.iLabel];
}

-(void)CaculateSize
{
	int fatherWidth;
	int width;
	CGRect rect;
	CGSize size;
	CGPoint pt;
	fatherWidth=self.iFatherWidth;
	width=fatherWidth*self.iWidthPt/100;
	rect.size.width=width;
//	rect.origin.x=0;
//	rect.origin.y=0;
	rect.size.height = self.iFatherHeight;
	self.bounds = rect;
	self.iLabel.bounds = rect;
	size=[self GetLabelHeght];//[iLabel GetLabelHeght];
	if (rect.size.height < size.height)
		rect.size.height=size.height;
	if(self.tag == 0)
		rect.size.width=width;
	else 
		rect.size.width=size.width;//超链接下划线用
	pt.x = rect.size.width / 2;
	pt.y = rect.size.height / 2;
	self.bounds = rect;
	self.center = pt;
	self.iLabel.bounds = rect;
	self.iLabel.center = pt;
}

- (CGSize) GetLabelHeght
{
	CGSize size;
	UIFont *font=self.iLabel.font;
	NSString *text=self.iLabel.text;
	size.height=2;
	size.width=self.bounds.size.width;
	
	if(text && [text length] > 0)
	{
		size.height=1000;
		size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
	}
	
	return size;
}

-(void)dealloc
{
	[iLabel release];
	iLabel = nil;
	[super dealloc];
}
@end

